package com.rqbl.controller;


import com.rqbl.entity.Bar;
import com.rqbl.entity.Post;
import com.rqbl.entity.User;
import com.rqbl.myutils.PageBean;
import com.rqbl.myutils.TableJson;
import com.rqbl.service.BarService;
import com.rqbl.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Controller
@RequestMapping("/post")
@CrossOrigin
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private BarService barService;

    @RequestMapping("getPostByPage")
    @ResponseBody
    public List<Post> getPostByPage(int page,Integer bid,Integer limit){
        PageBean pageBean = new PageBean();
        pageBean.setPage(page);
        pageBean.setPageSize(limit);
        return postService.getPostByPage(pageBean,bid);
    }

    @RequestMapping("getPostByPid")
    public String getPostByPid(Integer pid, HttpServletRequest request){
        Post post = postService.getPostByPid(pid);
        Bar bar =  barService.getBarById(post.getBid());
        request.setAttribute("post", post);
        request.setAttribute("bar", bar);
        return "forward:/forward/commentIndexPage";
    }


    /**
     * 查询数据，并以表格要求形式返回数据
     */
    @RequestMapping("getPostByPageTable")
    @ResponseBody
    public TableJson getPostByPageTable(int page, Integer bid, Integer limit){
        PageBean pageBean = new PageBean();
        pageBean.setPage(page);
        pageBean.setPageSize(limit);
        TableJson tableJson = new TableJson();
        tableJson.setCode(0);
        tableJson.setMsg("查询成功");
        tableJson.setData(postService.getPostByPage(pageBean,bid));
        tableJson.setCount(pageBean.getTotalSize());
        return tableJson;
    }


    /**
     * 根据ID删除某条评论,已经有触发器，无需考虑外键
     */
    @RequestMapping("deletePostById")
    @ResponseBody
    public String deletePostById(Integer pid){
        return postService.deletePostById(pid)>0? "success" : "fail" ;
    }


    @RequestMapping("getPostByPageUid")
    @ResponseBody
    public TableJson getPostByPageUid(int page,  Integer limit, HttpSession session){
        User user = (User) session.getAttribute("user");
        PageBean pageBean = new PageBean();
        pageBean.setPage(page);
        pageBean.setPageSize(limit);
        TableJson tableJson = new TableJson();
        tableJson.setData(postService.getPostByPageUid(pageBean,user.getUid()));
        tableJson.setCount(pageBean.getTotalSize());
        tableJson.setMsg("查询成功");
        tableJson.setCode(0);
        return tableJson;
    }

    @RequestMapping("addPost")
    @ResponseBody
    public String addPost(Integer bid,String title,String text,String url,HttpSession session){
        User user = (User) session.getAttribute("user");
        Post post = new Post();
        post.setBid(bid);
        post.setUid(user.getUid());
        post.setPtitle(title);
        post.setPtext(text);
        post.setPurl(url);
        return postService.addPost(post).toString();
    }
}
